package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;


    //根据用户名查询数据库获取用户信息和关联的角色信息,同事需要检查角色关联的权限信息
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);//查询用户基本信息,不包含用户的角色
        if (user == null) {
            return null;
        }
        Integer userId = user.getId();
        //根据用户的ID查询对应的角色
        Set<Role> roles = roleDao.findByUserId(userId);
        for (Role role : roles) {
            Integer roleId = role.getId();
            //根据角色ID查询关联的权限
            Set<Permission> permissions = permissionDao.findByRoleId(roleId);
            role.setPermissions(permissions);//让角色关联权限
        }
        user.setRoles(roles);//让用户关联角色
        return user;
    }

    @Override
    public void add(User user, Integer[] roleIds) {
        String password = user.getPassword();
        BCryptPasswordEncoder enc = new BCryptPasswordEncoder();
        String encode = enc.encode(password);
        user.setPassword(encode);
        userDao.add(user);
        Integer userId = user.getId();
        this.setUserAndRole(roleIds,userId);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);


        Page<CheckGroup> page = userDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    @Override
    public List<Integer> findRoleIdsByUserId(Integer id) {
        return userDao.findRoleIdsByUserId(id);
    }

    @Override
    public void edit(Integer[] roleIds, User user) {
        //修改检查主基本信息,操作检查组t_user表
        userDao.edit(user);
        //清理房钱检查组关联的检查项,操作中间关系表t_user_role表
        userDao.deleteAssocication(user.getId());
        //重新建立当前检查组的检查项关联关系
        Integer userId = user.getId();
        setUserAndRole(roleIds, userId);
    }

    @Override
    public void deleteById(Integer id) {
        userDao.deleteAssocication(id);
        userDao.deleteById(id);
    }


    private void setUserAndRole(Integer[] roleIds, Integer userId) {
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("user_id",userId);
                map.put("role_id",roleId);
                userDao.setUserAndRole(map);
            }
        }
    }
}
